#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
    vector<int> a={1,2,3,4,5,6,7,8};
    int count=1;
    while(1)
    {
        for(int c:a)
        {
            cout << c;
            if(count%8!=0)
                count<<' ';
            count++;
        }
        cout<<endl;
        if(!next_permutation(a.begin(),a.end()))
           break;
    }

    return 0;
}
